package com.zkx.fssb.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkx.fssb.constant.PasswordConstant;
import com.zkx.fssb.domain.dto.EmployeeDto;
import com.zkx.fssb.domain.po.Employee;
import com.zkx.fssb.domain.po.User;
import com.zkx.fssb.domain.vo.EmployeeVo;
import com.zkx.fssb.domain.vo.UserVo;
import com.zkx.fssb.exception.AccountNotFoundException;
import com.zkx.fssb.exception.BaseException;
import com.zkx.fssb.exception.LoginFailedException;
import com.zkx.fssb.mapper.EmployeeMapper;
import com.zkx.fssb.properties.JwtProperties;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.service.EmployeeService;
import com.zkx.fssb.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private JwtProperties jwtProperties;



    @Override
    public void customeSaveEmployee(EmployeeDto employeeDto) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getEmployeeName, employeeDto.getEmployeeName());
        if (this.getOne(lambdaQueryWrapper) != null){
            throw new BaseException("员工名已重复，无法添加");
        }

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto, employee);
        employee.setEmployeePassword(PasswordConstant.DEFAULT_PASSWORD);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        this.save(employee);


    }

    @Override
    public void customUpdateEmployee(EmployeeDto employeeDto) {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getEmployeeName, employeeDto.getEmployeeName());
//        if (this.getOne(lambdaQueryWrapper) != null){
//            throw new BaseException("员工名已重复，无法修改");
//        }

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto, employee);
        employee.setUpdateTime(LocalDateTime.now());
        this.updateById(employee);

    }

    @Override
    public Page customPage(PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageNo(),pageQuery.getPageSize());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getEmployeeName()!=null,Employee::getEmployeeName,pageQuery.getEmployeeName());
        this.page(page,wrapper);

        return page;

    }

    @Override
    public EmployeeVo customLogin(EmployeeDto employeeDto) {

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getEmployeeName,employeeDto.getEmployeeName());
        Employee employee = this.getBaseMapper().selectOne(lambdaQueryWrapper);
        if (employee==null){
            throw new AccountNotFoundException("账号不存在");
        }
        if (!employee.getEmployeePassword().equals(employeeDto.getEmployeePassword())){
            throw new LoginFailedException("员工名或密码错误");
        }

        EmployeeVo employeeVo = new EmployeeVo();
        BeanUtils.copyProperties(employee,employeeVo);

//        User user = this.getById(1);
//        log.info("user,{}",user);



        //TODO 登录成功后，需要生成token
        Map<String,Object> map = new HashMap<>();
        map.put("employeeId",employeeVo.getEmployeeId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), map);
        employeeVo.setToken(token);

        return employeeVo;
    }

}
